package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpShopBackgroundDTO;
import com.financing.platform.service.IFpShopBackgroundService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpShopBackgroundController")
@RequestMapping("/api/fpShopBackground")
public class FpShopBackgroundController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopBackgroundService fpShopBackgroundService;
	
	@RequestMapping("/listFpShopBackground")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopBackground list begin .......");
			
			fpShopBackground.setBegin(pager.getStartIndex());
			fpShopBackground.setPageSize(pager.getPageSize());
			
			int count = fpShopBackgroundService.queryListCount(fpShopBackground);
			
			pager.setTotalCount(count);
			
			List<FpShopBackgroundDTO> fpShopBackgroundList = null;
			
			if(count > 0){
				fpShopBackgroundList = fpShopBackgroundService.queryList(fpShopBackground);
			}else{
				fpShopBackgroundList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopBackgroundList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopBackground list end .......");
		}catch(Exception e){
			log.error("query fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopBackground")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopBackground  begin .......");
			
			DTOdateSet.setCreateInfo(fpShopBackground, request.getSession());
			DTOdateSet.setUpdateInfo(fpShopBackground, request.getSession());
			
			int id = fpShopBackgroundService.insert(fpShopBackground);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopBackground list end .......");
		}catch(Exception e){
			log.error("add fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopBackground")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopBackgroundDTO dto = fpShopBackgroundService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopBackground  end .......");
		}catch(Exception e){
			log.error("query detail fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopBackground")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopBackgroundService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopBackground  end .......");
		}catch(Exception e){
			log.error("delete fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopBackground")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
//			DTOdateSetUtil.setUpdateInfo(fpShopBackground);
			DTOdateSet.setUpdateInfo(fpShopBackground, request.getSession());
			
			fpShopBackgroundService.update(fpShopBackground);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopBackground list end .......");
		}catch(Exception e){
			log.error("update fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopBackground")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
//			DTOdateSetUtil.setUpdateInfo(fpShopBackground);
			
			fpShopBackgroundService.updateForce(fpShopBackground);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopBackground list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
